package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Physical;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.BCryptPasswordEncoderUtils;
import com.itheima.utils.MD5Utils;
import com.qiniu.util.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author 吴
 * @version V1.0
 * @Description
 * @Package com.itheima.service.Impl
 * @date 2021/11/28 14:40
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    /**
     * 通过用户名查询信息
     * @param username 用户名
     * @return
     */
    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    /**
     * @Description:  根据条件查询（分页查询）
     * @Param:  queryPageBean
     * @return:  PageResult
     * @Author: 文攀
     * @Date: 2021/12/8
     */
    @Override
    public PageResult selectByCondition(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<User> users=userDao.selectByCondition(queryPageBean.getQueryString());
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public List<Role> findRoles() {
        return userDao.findRoles();
    }


    @Override
    public void add(User user, Integer[] roles) {
        String password = user.getPassword();
        if (!password.isEmpty()){
            password= BCryptPasswordEncoderUtils.encodePassword(password);
            user.setPassword(password);
        }
        //添加用户到用户表中
        userDao.add(user);
        //判断角色是否为空
        if (roles!=null && roles.length>0){
            Integer userId = user.getId();
            userDao.setUserAndRoles(userId,roles);
        }


    }

    @Override
    public User findById(Integer userId) {
        return userDao.findById(userId);
    }

    @Override
    public Integer[] findRolesByUserId(Integer userId) {
        return userDao.findRolesByUserId(userId);
    }

    @Override
    public void update(User user, Integer[] roleIds) {
        String password = user.getPassword();
        if (password!=null && password!=""){
            String password1 = BCryptPasswordEncoderUtils.encodePassword(password);
            user.setPassword(password1);
        }
        //修改用户信息
        userDao.update(user);

        //删除原有的用户跟角色关系
        userDao.deleteRoleUserByUserId(user.getId());

        //判断角色id是否为空
        if (roleIds !=null && roleIds.length>0){
            //重新添加用户跟角色之间的关系
            userDao.setUserAndRoles(user.getId(),roleIds);
        }
    }

    @Override
    public void deleteById(Integer userId) {
        //删除用户跟角色的关系
        userDao.deleteRoleUserByUserId(userId);
        //根据id删除用户
        userDao.deleteById(userId);
    }

    @Override
    public PageResult findPhysical(QueryPageBean queryPageBean) {
       PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Physical> physicals=userDao.findPhysical();
        PageInfo<Physical> pageInfo = new PageInfo<>(physicals);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }
}
